
<?php
ini_set("memory_limit", "1024M");
require dirname(__FILE__).'/../core/init.php';

/* Do NOT delete this comment */
/* 不要删除这段注释 */

$configs = array(
    'name' => 'pixabay',
   // 'log_show' => true,
    'tasknum' => 1,
    //'save_running_state' => true,
    'domains' => array(
        'pixabay.com',
        'www.pixabay.com'
    ),
    'scan_urls' => array(
        'https://pixabay.com/zh/photos/'
    ),
    'list_url_regexes' => array(
        "https://pixabay.com/zh/photos/?orientation=&image_type=&cat=&colors=&q=&order=popular&pagi=\d+"
    ),
    'content_url_regexes' => array(
        "",
    ),
    'max_try' => 5,
    //'export' => array(
    //'type' => 'csv',
    //'file' => PATH_DATA.'/qiushibaike.csv',
    //),
    /*  'export' => array(
      'type'  => 'sql',
      'file'  => PATH_DATA.'/qiushibaike.sql',
      'table' => 'content',
      ),*/
    /*'export' => array(
        'type' => 'db',
        'table' => 'content',
    ),*/
    'fields' => array(

        array(
            'name' => "article_headimg",
            'selector' => "//div[contains(@class,'item')]//a[1]",
            'required' => true,
        ),

    ),
);

$spider = new phpspider($configs);

$spider->on_handle_img = function($fieldname, $img)
{
    $regex = '/src="(https?:\/\/.*?)"/i';
    preg_match($regex, $img, $rs);
    if (!$rs)
    {
        return $img;
    }

    $url = $rs[1];
    $url = str_replace("__340","_1280",$url);
    $img = $url;
    if (stripos($url,'blank.gif') > 0   || stripos($url,'x250') >0 || stripos($url,'_250') >0 || stripos($url,'__180') >0) {
        //过滤 无效,损坏和尺寸不符合的图片
        return true;
    } else {
        
        $pathinfo = pathinfo($url);
        $fileext = $pathinfo['extension'];
        if (strtolower($fileext) == 'jpeg')
        {
            $fileext = 'jpg';
        }

        $filename = uniqid().".".$fileext;

        $filepath = "images/{$filename}";

        $arr = parse_url($url);

        $file = basename($arr['path']);

        $image = file_get_contents($url);

        if (!file_exists("'images/'.$file")) {
            file_put_contents('images/'.$file, $image);
        }


        // exec("wget -q {$url} -O {$filepath}");


        $img = str_replace($url, $filename, $img);
        return $img;
    }
      
 
    

};
 
$spider->start();




 